package cn.edu.imnu.Service.impl;



import cn.edu.imnu.Service.DoctorService;
import cn.edu.imnu.entity.Result;
import cn.edu.imnu.entity.dto.*;
import cn.edu.imnu.entity.po.Account;
import cn.edu.imnu.entity.po.Doctor;
import cn.edu.imnu.entity.vo.DoctorListvo;
import cn.edu.imnu.entity.vo.Doctorlevelvo;
import cn.edu.imnu.entity.vo.SaleListvo;
import cn.edu.imnu.entity.vo.TreatTypevo;
import cn.edu.imnu.mapper.AccountMapper;
import cn.edu.imnu.mapper.DoctorLevelMapper;
import cn.edu.imnu.mapper.DoctorMapper;
import cn.edu.imnu.mapper.TreatTypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【doctor】的数据库操作Service实现
* @createDate 2025-06-08 18:08:13
*/
@Service
@MapperScan("cn/edu/imnu/mapper")
public class DoctorServiceImpl
implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private DoctorLevelMapper doctorLevelMapper;
    @Autowired
    private TreatTypeMapper treatTypeMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public DoctorListvo getAllDoctors(DoctorListdto dto) {
        // 设置分页参数（currentPage和pageSize可能为null，需做默认值处理）
        Integer currentPage = dto.getCurrentPage() != null ? dto.getCurrentPage() : 1;
        Integer pageSize = dto.getPageSize() != null ? dto.getPageSize() : 10;
        // 启动分页（PageHelper会自动拦截后续的查询SQL，添加LIMIT语句）
        PageHelper.startPage(currentPage, pageSize);
        // 执行查询（实际会被PageHelper改写为：SELECT * FROM sale LIMIT (currentPage-1)*pageSize, pageSize）
        List<DoctorListdto> doctorList = doctorMapper.selectAllDoctors(dto);
        // 通过PageInfo获取分页信息（总记录数、总页数等）
        PageInfo<DoctorListdto> pageInfo = new PageInfo<>(doctorList);
        // 封装结果到SaleListvo
        DoctorListvo doctorListvo = new DoctorListvo();
        doctorListvo.setList(pageInfo.getList());       // 分页后的数据列表
        doctorListvo.setTotal(pageInfo.getTotal());     // 总记录数
        doctorListvo.setCurrentPage(pageInfo.getPageNum());  // 当前页
        doctorListvo.setPageSize(pageInfo.getPageSize());    // 每页条数
        return  doctorListvo;
       /* return doctorMapper.selectAllDoctors();*/
    }

    @Override
    public Result deletedoctor(Long id) {
        if (id == null || id <= 0) {
            return Result.error("医生 ID 不合法");
        }

        // 执行删除操作
        int affectedRows = doctorMapper.deleteByPrimaryKey(id);

        // 根据结果返回响应
        if (affectedRows > 0) {
            return Result.success("医生信息删除成功");
        } else {
            return Result.error("未找到 ID 为 " + id + " 的医生");
        }
    }
   //医生等级列表
    @Override
    public List<Doctorlevelvo> getDoctorslevel() {
        return doctorLevelMapper.selectDoctorslevel();
    }

    @Override
    public List<TreatTypevo> getTreatType() {
        return treatTypeMapper.selectTreatType();
    }

    @Override
    public Result updateDoctor(Doctorupdatedto dto) {
        if (dto.getId() == null) {
            return Result.error("id 不能为空");
        }

        // 2. DTO 转 PO（Sale）
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(dto, doctor);

        // 3. 调用 Mapper 动态更新
        int rows = doctorMapper.updateByPrimaryKeySelective(doctor);
        if (rows == 0) {
            return Result.error("未找到对应记录，修改失败");
        }

        return Result.success("修改成功");
    }

    @Override
    public Result updateDoctorpwd(Updatepwddto dto) {
        if (dto.getId() == null) {
            return Result.error("id 不能为空");
        }

        // 2. DTO 转 PO（Sale）
        Doctor doctor = new Doctor();
        Account account = new Account();
        BeanUtils.copyProperties(dto, account);

        // 3. 调用 Mapper 动态更新
        int rows = accountMapper.updatePasswordByDoctorId(dto);
        if (rows == 0) {
            return Result.error("未找到对应记录，修改失败");
        }

        return Result.success("修改成功");
    }

    @Override
    public Result addDoctor(Doctoradddto dto) {
        if (dto.getName() == null || dto.getPhone() == null) {
            return Result.error("姓名和电话不能为空");
        }
        Date now = new Date();
        dto.setCreatetime(now);
        dto.setUpdatetime(now);
        // 执行插入
        int count = doctorMapper.insertDoctor(dto);
        if (count > 0) {
            return Result.success("信息添加成功");
        }
        return Result.error("信息添加失败");
    }
}
